package com.hp.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hp.entity.Type;
import com.hp.service.ITypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @Author: LiuQiang
 * @Date: 2021/10/23 22:00
 */
@Controller
@RequestMapping("/admin")
public class TypeController {

    @Autowired
    private ITypeService typeService;

    /**
     * 查询类目信息
     * @param page 页码
     * @return
     */
    @GetMapping("/typeList")
    public String typeList(int page, Model model){
        PageHelper.startPage(page,5);
        //查询全部信息
        List<Type> typeList = typeService.findTypeAll();
        //将信息添加PageInfo
        PageInfo pageInfo = new PageInfo(typeList);
        model.addAttribute("typeList",typeList);
        //model传值
        model.addAttribute("pageInfo",pageInfo);
        return "admins/type_list";
    }

    /**
     * 跳转修改页面
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/typeEdit")
    public String typeEdit(@RequestParam("id") Integer id, Model model){
        //根据id查询出数据Type
        Type type = typeService.findTypeById(id);
        //model传值
        model.addAttribute("type",type);
        return "admins/type_edit";
    }

    /**
     * 修改types类目表数据
     * @param type
     * @return
     */
    @PostMapping("/typeUpdate")
    public String typeUpdate(Type type){
        //修改type
        typeService.typeUpdate(type);
        //需在service层判断修改名称有无重复
        return "redirect:/admin/typeList?page=1";
    }

    /**
     * 根据id删除types类目表信息
     * @param id
     * @return
     */
    @GetMapping("/typeDelete")
    public String typeDelete(@RequestParam("id") Integer id){
        //删除
        typeService.typeDeleteById(id);
        return "redirect:/admin/typeList?page=1";
    }

    /**
     * 跳转新增页面
     * @return
     */
    @GetMapping("/toAddTypes")
    public String toAddTypes(){
        return "admins/type_add";
    }

    /**
     * 新增types表类目信息
     * @param type
     * @return
     */
    @PostMapping("/addTypes")
    public String addTypes(Type type){
        //新增信息
        typeService.addTypes(type);
        return "redirect:/admin/typeList?page=1";
    }

}
